<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * 商品评论模型
 *
 * @property int $id 主键ID
 * @property int $uniacid 公众号ID
 * @property int $order_id 订单ID
 * @property int $goods_id 商品ID
 * @property int $uid 用户ID
 * @property string $nick_name 用户昵称
 * @property string $head_img_url 用户头像
 * @property string $content 评论内容
 * @property int $level 评分等级(1-5)
 * @property string $images 评论图片(序列化)
 * @property int $comment_id 父评论ID(0表示一级评论)
 * @property int $type 评论类型
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property string $deleted_at 删除时间
 */
class Comment extends Model
{
    use SoftDeletes;

    protected $table = 'ims_yz_comment';
    
    protected $fillable = [
        'uniacid',
        'order_id',
        'goods_id',
        'uid',
        'nick_name',
        'head_img_url',
        'content',
        'level',
        'images',
        'comment_id',
        'type'
    ];

    protected $hidden = [];
    
    protected $casts = [
        'id' => 'integer',
        'uniacid' => 'integer',
        'order_id' => 'integer',
        'goods_id' => 'integer',
        'uid' => 'integer',
        'level' => 'integer',
        'comment_id' => 'integer',
        'type' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime'
    ];

    /**
     * 获取评论回复
     */
    public function hasManyReply(): HasMany
    {
        return $this->hasMany(Comment::class, 'comment_id', 'id');
    }

    /**
     * 获取关联商品
     */
    public function hasOneGoods(): BelongsTo
    {
        return $this->belongsTo(Goods::class, 'goods_id', 'id');
    }

    /**
     * 获取图片数组
     */
    public function getImagesArrayAttribute()
    {
        if (empty($this->images)) {
            return [];
        }
        
        $images = @unserialize($this->images);
        if (!is_array($images)) {
            return [];
        }
        
        return $images;
    }

    /**
     * 设置图片数组
     */
    public function setImagesArrayAttribute($value)
    {
        if (is_array($value)) {
            $this->attributes['images'] = serialize($value);
        }
    }

    /**
     * 根据商品ID获取评论
     */
    public static function getByGoodsIds($goodsIds)
    {
        return static::whereIn('goods_id', $goodsIds)
            ->where('comment_id', 0) // 只查询一级评论
            ->where('type', 1); // 商品评论类型
    }

    /**
     * 获取评论总数
     */
    public static function getCommentTotal($goodsIds)
    {
        return static::getByGoodsIds($goodsIds)->count();
    }

    /**
     * 获取评分总和
     */
    public static function getScoreTotal($goodsIds)
    {
        return static::getByGoodsIds($goodsIds)->sum('level');
    }

    /**
     * 获取平均评分
     */
    public static function getAverageScore($goodsIds)
    {
        $commentTotal = static::getCommentTotal($goodsIds);
        if ($commentTotal == 0) {
            return 5; // 默认5分
        }
        
        $scoreTotal = static::getScoreTotal($goodsIds);
        return bcdiv($scoreTotal, $commentTotal, 1);
    }
}
